package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.listener.DoctoralSupervisorListener;
import com.TeacherInfoServer.pojo.DTO.AddDoctoralSupervisorDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.DoctoralSupervisorSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateDoctoralSupervisorDTO;
import com.TeacherInfoServer.pojo.excelImport.DoctoralSupervisorExcel;
import com.TeacherInfoServer.pojo.vo.DoctoralSupervisorListVo;
import com.TeacherInfoServer.common.service.DoctoralSupervisorService;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/s1/doctoral_supervisor")
public class DoctoralSupervisorController {

    @Autowired
    private DoctoralSupervisorService doctoralSupervisorService;
    @Autowired
    private DoctoralSupervisorListener doctoralSupervisorListener;

    @PostMapping("/addOne")
    public Result<Map<String, Integer>> addOne(@RequestBody @Valid AddDoctoralSupervisorDTO addDoctoralSupervisorDTO){
        return doctoralSupervisorService.addOne(addDoctoralSupervisorDTO);
    }

    @PostMapping("/addBatch")
    public Result<Map<String,Integer>> addBatch(@RequestBody MultipartFile file) throws IOException{
        doctoralSupervisorListener.initList();
        EasyExcel.read(file.getInputStream(), DoctoralSupervisorExcel.class,doctoralSupervisorListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",doctoralSupervisorListener.getRecord());
        return Result.success(map);
    }

    @GetMapping(value = "/getList",params = {"pageNumber","pageSize"})
    public Result<DoctoralSupervisorListVo> getList(@RequestParam Long pageNumber,@RequestParam Long pageSize){
        return doctoralSupervisorService.getList(pageNumber, pageSize);
    }

    @Operation(summary = "模糊查询获取博士生导师信息列表",description = "模糊查询分页获取博士生导师信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody DoctoralSupervisorSearchDTO doctoralSupervisorSearchDTO){
        return Result.success("获取博士生导师列表成功",doctoralSupervisorService.searchDoctoralSupervisorList(doctoralSupervisorSearchDTO));
    }

    @GetMapping(value = "/export",params = "fileName")
    public void export(HttpServletResponse response, @RequestParam String fileName) throws IOException{
        doctoralSupervisorService.export(response,fileName);
    }

    @DeleteMapping(value = "/delete",params = "teacherIds")
    public Result<Map<String, Integer>> deleteBatch(@RequestParam String[] teacherIds){
        return doctoralSupervisorService.deleteBatch(teacherIds);
    }

    @PutMapping("/update")
    public Result<Map<String, Integer>> updateOne(@RequestBody @Valid UpdateDoctoralSupervisorDTO updateDoctoralSupervisorDTO){
        return doctoralSupervisorService.updateOne(updateDoctoralSupervisorDTO);
    }
}
